package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsTypeService;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public String getGoodsTypes() {
        //返回数据
        return JSON.toJSONString(this.getChilds(-1));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveGoodsType(String goodsTypeName, Integer pId) {
        //插入商品分类
        GoodsType goodsType = new GoodsType(goodsTypeName, 0, pId);
        goodsTypeDao.insertGoodsType(goodsType);
        //更新父节点的状态为1
        GoodsType goodsTypeParent = new GoodsType(null, 1, null);
        goodsTypeParent.setGoodsTypeId(pId);
        goodsTypeDao.updateGoodsTypeState(goodsTypeParent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeGoodsType(Integer goodsTypeId) {
        /**
         * 删除并判断父节点下是否还有子节点
         */
        GoodsType goodsType = goodsTypeDao.selectById(goodsTypeId);
        goodsTypeDao.deleteGoodsTypeById(goodsTypeId);

        Integer pId = goodsType.getPId();
        List<GoodsType> goodsTypes = goodsTypeDao.selectByPid(pId);

        if (CollectionUtils.isEmpty(goodsTypes)) {
            //更新父节点的状态为0
            GoodsType goodsTypeParent = new GoodsType(null, 0, null);
            goodsTypeParent.setGoodsTypeId(pId);
            goodsTypeDao.updateGoodsTypeState(goodsTypeParent);
        }
    }

    /**
     * 根据父id获取分类信息
     *
     * @param pid
     * @return
     */
    public List<Map<String, Object>> getChilds(Integer pid) {
        //根据父id获取子列表
        List<GoodsType> childList = goodsTypeDao.selectByPid(pid);
        if (!CollectionUtils.isEmpty(childList)) {
            //遍历结果
            List<Map<String, Object>> result = new ArrayList<>();
            for (GoodsType goodsType : childList) {
                Map<String, Object> map = new LinkedHashMap<>();
                map.put("id", goodsType.getGoodsTypeId());
                map.put("text", goodsType.getGoodsTypeName());
                map.put("iconCls", "goods-type");
                //获取属性值
                Map<String, Object> attributes = new HashMap<>();
                attributes.put("state", goodsType.getGoodsTypeState());
                map.put("attributes", attributes);
                //判断closed/open
                if (goodsType.getGoodsTypeState() == 0) {
                    map.put("state", "open");
                } else {
                    map.put("state", "closed");
                    //获取下级节点信息
                    List<Map<String, Object>> childs = this.getChilds(goodsType.getGoodsTypeId());
                    if (!CollectionUtils.isEmpty(childs)) {
                        map.put("children", childs);
                    }
                }
                result.add(map);
            }
            return result;
        } else {
            return null;
        }
    }


}
